package com.sky.service.impl;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.BusinessException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.utils.BeanHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Api(tags = "分类管理")
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public PageResult page(CategoryPageQueryDTO pageQueryDTO) {
        //1.设置分页参数
        PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());

        //2.执行查询
        List<Category> categoryList = categoryMapper.pageQuery(pageQueryDTO);

        //3.解析并返回
        Page<Category> page = (Page<Category>) categoryList;

        return new PageResult(page.getTotal(), page.getResult());
    }

    //修改分类
    @Override
    public void update(CategoryDTO categoryDTO) {
        log.info("修改分类：{}", categoryDTO);
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());

        //属性拷贝
        Category category = BeanHelper.copyProperties(categoryDTO, Category.class);
        categoryMapper.update(category);
        return;
    }

    //根据id查询分类
    @Override
    public Category getById(Long id) {
        log.info("根据id查询分类：{}", id);
        Category categoryById = categoryMapper.selectById(id);
        return categoryById;
    }

    //启用或禁用分类的状态
    @Override
    public void updateStatus(Integer status, Long id) {
        log.info("启用或禁用分类id为：{}", id);
        Category category = Category.builder()
                        .status(status)
                        .id(id)
                        .build();
        categoryMapper.update(category);
    }

    //新增菜品
    @Override
    public void add(CategoryDTO categoryDTO) {
        Category category = BeanHelper.copyProperties(categoryDTO, Category.class);

        //设置初始状态为禁用状态0
        category.setStatus(StatusConstant.DISABLE);
        //设置创建时间和其他创建信息
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.insert(category);
    }

    //根据id删除分类
    @Override
    public void delete(Long id) {
        //1. 查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        Long count = dishMapper.countByCategoryId(id);
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new BusinessException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //2. 查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        count = setmealMapper.countByCategoryId(id);
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new BusinessException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //3. 删除分类数据
        categoryMapper.deleteById(id);
    }


    //根据类型查询分类
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }


}
